/**
 * 详情对话框
 */
var CbAboutUsInfoDlg = {
    data: {
        id: "",
        logo: "",
        introduction: "",
        website: "",
        serviceAgreement: "",
        privacyAgreement: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects', 'layedit'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layedit = layui.layedit;

    //普通图片上传
    var upload = layui.upload;
    //普通图片上传
    upload.render({
        elem: '#imagesUpload'
        ,url: Feng.ctxPath + '/api/oss/upload_file' //改成您自己的上传接口
        ,before: function(obj){
            console.log("upload img obj: "+ obj);
            // 预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            console.log("upload img data: "+ res.data);
            var fileId = res.data;
            // $("#images").val(fileId);
            var ajax = new $ax(Feng.ctxPath + "/system/updateAvatar", function (data) {
                Feng.success(res.message);
                $("#logo").val(fileId);
            }, function (data) {
                Feng.error("修改失败!" + data.responseJSON.message + "!");
            });
            ajax.set("fileId", res.data);
            ajax.start();
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });















    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/api/aboutUs/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();

    var images = result.data.logo;
    $('#filePreview').attr('src', Feng.ctxPath+'/api/oss/stream_file?objectName='+images); //图片链接（base64）

    var introduction = decodeURIComponent(result.data.introduction);
    var serviceAgreement = decodeURIComponent(result.data.serviceAgreement);
    var privacyAgreement = decodeURIComponent(result.data.privacyAgreement);
    // console.log("introduction: "+ introduction);
    form.val('cbAboutUsForm', result.data);
    // layedit.setContent(index,introduction);
    // $("#introduction").val(introduction);


    var index_introduction = layedit.build('introduction',
        {
            tool: ["strong", "italic", "underline", "del", "|", "left", "center", "right"],
            // height: 600
        }); //建立编辑器

    var index_service = layedit.build('serviceAgreement',
        {
            tool: ["strong", "italic", "underline", "del", "|", "left", "center", "right"],
        }); //建立编辑器

    var index_privacy = layedit.build('privacyAgreement',
        {
            tool: ["strong", "italic", "underline", "del", "|", "left", "center", "right"],
        }); //建立编辑器

    layedit.setContent(index_introduction, introduction);//赋值
    layedit.setContent(index_service, serviceAgreement);//赋值
    layedit.setContent(index_privacy, privacyAgreement);//赋值

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var introductionContent = layedit.getContent(index_introduction);
        var serviceAgreementContent = layedit.getContent(index_service);
        var privacyAgreementContent = layedit.getContent(index_privacy);

        var ajax = new $ax(Feng.ctxPath + "/api/aboutUs/editItem", function (data) {
            Feng.success("更新成功！");
            window.location.href = Feng.ctxPath + '/api/aboutUs'
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        data.field.introduction = encodeURIComponent(introductionContent);
        data.field.serviceAgreement = encodeURIComponent(serviceAgreementContent);
        data.field.privacyAgreement = encodeURIComponent(privacyAgreementContent);
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $('#cancel').click(function(){
        window.location.href = Feng.ctxPath + '/api/aboutUs'
    });
});